package bdddaosimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bdd.beans.Contratquadriennal;
import bdd.beans.DBLink;
import bdd.beans.Indisponibilites;
import bdd.beans.Periodes;
import bdddaos.DAOPeriodes;
import bdddaos.DAOPeriodes;

public class DAOPeriodesImpl implements DAOPeriodes {

	private DBLink dblink;

	public DAOPeriodesImpl() {		
		super();
		this.dblink = new DBLink(); 
	}

	public Periodes save(Periodes p) {
		try {
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("INSERT INTO Periodes (numpromotion, datedebut, datefin)" +
					" VALUES ("+p.getPromotion().getNumpromotion()+ ", "+ p.getJoursByDatedebut().getIddate() +", "+p.getJoursByDatefin().getIddate() +")"));
			this.dblink.getPst().executeQuery();
			DAOOutils outil = new DAOOutils();
			p.setNumperiode(outil.findMaxId("Periodes", "NUMPERIODE"));
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te");
		}
		finally{
			this.dblink.deconnexion();
		}
		return p;
	}


	public Periodes find(Integer id) {
		Periodes unePeriode = new Periodes();
		unePeriode.setNumperiode(id);
		try {
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("SELECT * FROM PERIODES WHERE NUMPERIODE = " + id));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				unePeriode.setPromotion(new DAOPromotionImpl().find(rs.getInt("NUMPROMOTION")));
				unePeriode.setJoursByDatedebut(new DAOJoursImpl().find(rs.getDate("DATEDEBUT")));
				unePeriode.setJoursByDatefin(new DAOJoursImpl().find(rs.getDate("DATEFIN")));
			}
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te");
		}
		return unePeriode;
	}

	public List<Periodes> findAll() {
		List<Periodes> Lstretour = new ArrayList();
		try {
			this.dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("SELECT * FROM PERIODES"));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				Periodes retour = new Periodes();
				retour.setNumperiode(rs.getInt("NUMPERIODE"));
				retour.setPromotion(new DAOPromotionImpl().find(rs.getInt("NUMPROMOTION")));
				retour.setJoursByDatedebut(new DAOJoursImpl().find(rs.getDate("DATEDEBUT")));
				retour.setJoursByDatefin(new DAOJoursImpl().find(rs.getDate("DATEFIN")));
				Lstretour.add(retour);
			}		
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te" + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return Lstretour;
	}

	public void update(Periodes o) {

		try {
			this.dblink.connexion();
			String r = "UPDATE PERIODES SET DATEDEBUT='"+o.getJoursByDatedebut().getIddate()+ "', DATEFIN='"+o.getJoursByDatefin().getIddate() + "', NUMPROMOTION='"+o.getPromotion().getNumpromotion() + "' WHERE NUMPERIODE="+o.getNumperiode();
			
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
	}

	public void delete(Periodes o) {
		try {
			this.dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("DELETE FROM PERIODES WHERE NUMPERIODE = " + o.getNumperiode()));
		} catch (SQLException e) {
			System.out.println("Erreur de requ�te : " + e);
		}
		finally{
			this.dblink.deconnexion();
		}
	}

}
